﻿@model TopicModel
@using SmartStore.Web.Models.Topics;

@if (!Model.Body.Value.HasValue())
{
    return;
}

@if (Model.IsPasswordProtected)
{
    <script type="text/javascript">    
        $(document).ready(function () {
            $('#ph-topic-@Model.Id').hide();            
            $('#ph-password-@Model.Id #password-@Model.Id').select().focus();
        });

        function OnAuthenticateSuccess@(Model.Id)(context) {
            if (context.Authenticated)
            {
                $('#ph-title-@Model.Id .topic-html-content-title h2.topic-html-content-header').html(context.Title);
                if ($('#ph-title-@Model.Id .topic-html-content-title h2.topic-html-content-header').text().length == 0)
                {
                    $('#ph-title-@Model.Id').hide();
                }
                $('#ph-topic-@Model.Id .topic-html-content-body').html(context.Body);
                $('#ph-password-@Model.Id').hide();
                $('#ph-topic-@Model.Id').show();
            }
            else
            {
                $('#password-error-@Model.Id').text(context.Error);
                $('#ph-password-@Model.Id #password-@Model.Id').select().focus();
            }
        }
    </script>
    <div id="ph-password-@Model.Id">
        @using (Ajax.BeginForm("Authenticate", new AjaxOptions
        {
            HttpMethod = "Post",
            OnSuccess = "OnAuthenticateSuccess" + @Model.Id,
            LoadingElementId = "authenticate-progress-" + @Model.Id
        }))
        {
            @Html.HiddenFor(model => model.Id)
            <div class="enter-password-title">
                @T("Topic.EnterPassword")
            </div>
            <div class="enter-password-form">
                @Html.Password("password", null, new { id = "password-" + @Model.Id })
                <input type="submit" value="@T("Topic.Button")" class="button-1 topic-password-button"/>
                <span id="authenticate-progress-@Model.Id" style="display: none;">@T("Common.Wait...")
                    <img src='@Url.Content("~/Content/Images/ajax_loader_small.gif")' alt="@T("Common.Wait...")" />
                </span>
            </div>
            <div class="password-error">
                <span id="password-error-@Model.Id"></span>
            </div>
        }
    </div>
}

@if (ViewBag.BodyOnly)
{
    <div class="topic-html-content-body@(ViewBag.IsLead == true ? " lead" : "") my-3 html-editor-content" id="ph-topic-body-@Model.Id" @Html.LanguageAttributes(Model.Body)>
        @Html.Raw(Html.CollapsedText(Model.Body))
    </div>
}
else
{
    <div id="ph-topic-@Model.Id" class="block topic-html-content my-3">
        @if (Model.Title.HasValue())
        {
            <div class="block-title" id="ph-title-@Model.Id">
				@if (ViewBag.IsLead == true)
				{
					<h1 class="h2" @Html.LanguageAttributes(Model.Title)>@Model.Title</h1>
				}
				else
				{
					<h4 @Html.LanguageAttributes(Model.Title)>@Model.Title</h4>
				}
            </div>
        }

        <div class="block-body html-editor-content topic-html-content-body@(ViewBag.IsLead == true ? " lead" : "")" @Html.LanguageAttributes(Model.Body)>
			@if (Model.Intro.HasValue() && (ViewBag.IsLead == null || ViewBag.IsLead == false))
			{
				<p class="topic-intro lead">
					@Html.Raw(Model.Intro)
				</p>
			}

            @Html.Raw(Html.CollapsedText(Model.Body))
        </div>
    </div>
}